package com.example.demo.control;

import com.example.demo.model.Result;
import com.example.demo.model.buisness;
import com.example.demo.model.dish;
import com.example.demo.model.user;
import com.example.demo.service.buisnessService;
import com.example.demo.service.dishService;
import com.example.demo.service.userService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@Controller
public class buisnessController {
    @Autowired
    private static final Logger LOG = LoggerFactory.getLogger(buisnessController.class);
    @Autowired
    private dishService dishService;
    @Autowired
    private userService userService;
    @Autowired
    private buisnessService buisnessService;
    @PostMapping("/api/buisness/add")
    public String add(Model model, @RequestParam("buisnessName") String buisnessName){
        buisness buisness = new buisness();
        buisness = buisnessService.findByBuisnessName(buisnessName);
        if (buisness == null){
            return null;
        }
        buisness.setBname(buisnessName);

        int[] numbers = {13,14,15};
        String randomImageName = generateRandomImageName(numbers);

        buisness.setImages("./images/"+randomImageName);
        int add = buisnessService.add(buisness);
        if (add!=0){
            model.addAttribute("datalist1",buisnessService.getAllBuisness());
            return "redirect:/index";
        }

        return null;
    }
    @PostMapping("/api/buisness/delete")
    public String delete(@RequestParam("buisnessName") String buisnessName, Model model){
        buisness buisness = buisnessService.findByBuisnessName(buisnessName);
        Result result = new Result();
        if (buisness!=null){
            buisnessService.delete(buisness.getBid());
            model.addAttribute("datalist1",buisnessService.getAllBuisness());
            result.setSuccess(true);
            return "redirect:/index";
        }
        result.setSuccess(false);
        return null;
    }



    @PostMapping("/api/addBuisness")
    public String addDish(@RequestParam("buisnessName") String buisnessName, Model model){
        if (!StringUtils.hasText(buisnessName)){
            LOG.error("商家名称为空");
            return null;
        }

        buisness buisness = new buisness();
        buisness.setBname(buisnessName);
        int[] numbers = {13,14,15};
        String randomImageName = generateRandomImageName(numbers);

       buisness.setImages("./images/"+randomImageName);
        buisnessService.add(buisness);
        model.addAttribute(buisnessService.getAllBuisness());
        return "redirect:/index";
    }
    public static String generateRandomImageName(int[] numbers) {
        // 创建一个随机数生成器
        Random random = new Random();

        // 从数组中随机选择一个数字
        int randomIndex = random.nextInt(numbers.length);
        int selectedNumber = numbers[randomIndex];

        // 组合成字符串
        String imageName = selectedNumber + ".jpg";

        return imageName;
    }
}
